package com.gdqy.FCS.web;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gdqy.FCS.common.api.FastResponse;
import com.gdqy.FCS.common.api.ListResponse;
import com.gdqy.FCS.common.mvc.Args;
import com.gdqy.FCS.entity.Comment;
import com.gdqy.FCS.entity.Dictionary;
import com.gdqy.FCS.entity.Information;
import com.gdqy.FCS.entity.User;
import com.gdqy.FCS.service.CommentService;
import com.gdqy.FCS.service.DictionaryService;
import com.gdqy.FCS.service.InformationService;
import com.gdqy.FCS.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("information")
public class InformationController {
	
	@Autowired 
	private InformationService informationService;
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private UserService userService;
	@Autowired
	private CommentService commentService;
	
	
/**************************************************************	管理资讯Start******************************************************************/
	@RequestMapping("admin_info")
	public ModelAndView  admin_info(int pageindex,int pagesize,Args args,String status,String type_code) {
		ModelAndView modelAndView = new ModelAndView();
		StringBuffer path = new StringBuffer("admin");
		
		// 获取服装--分页
				PageHelper.startPage(pageindex, pagesize);
			List<Information> informations = informationService.selectByStatusAndTpye_code(status,type_code);
				PageInfo<Information> page = new PageInfo<Information>(informations);
				ListResponse<Information> json = FastResponse._().errCode(0).subCode(0)
						.errMsg("获取列表成功").args(args.getReqeustargs())
						.pageIndex(page.getPageNum()).pageSize(page.getSize())
						.total((int) page.getTotal()).pageCount(page.getPages())
						.hasNext(page.isHasNextPage()).list(informations);
				modelAndView.addObject("json", json);
				if(type_code.equals("ZD002001")) {
					if(status.equals("1")) {
						path.append("/info_released");
					}else if(status.equals("2")) {
						path.append("/info_unreleased");
					}
				}else {
					if(status.equals("1")) {
						path.append("/marketInfo_released");
					}else if(status.equals("2")) {
						path.append("/marketInfo_unreleased");
					}
				}
			
		modelAndView.setViewName(path.toString());
		return modelAndView;
	}
	//通过审核
	@RequestMapping("pass")
	public @ResponseBody String pass(int info_id) {
		String msg="fail";
		Information info= informationService.selectById(info_id);
		if(info != null) {
			if(info.getStatus().equals("2")) {
				info.setRelease_time(new Date());
				info.setStatus("1");
				info.setModify_time(new Date());
				int data = informationService.update(info);
				if(data>0) {
					msg="success";
				}
			}
		}
		return msg;
	}
	//删除资讯
	@RequestMapping("delete")
	public @ResponseBody String delete(@RequestParam(value = "user_id",required = true) int user_id,
			@RequestParam(value = "info_id",required = true)int info_id){
		String msg = "fail";

		User user = userService.selectById(user_id);
		if(user!=null) {
			//判断角色
			if(user.getRole().equals("admin")) {
				int data = informationService.deleteById(info_id);
				if(data>0) {
					msg="success";
				}
			}else {
				Information info = informationService.selectById(info_id);
				if(info.getRelease_id() == user_id) {
					int data = informationService.deleteById(info_id);
					if(data>0) {
						msg="success";
					}
				}else {
					msg="你没有权限删除该资讯";
				}
			}
		}else {
			msg="你没有权限删除该资讯";
		}
		return msg;
	}
	
/**************************************************************	管理资讯end******************************************************************/
	//处理修改咨询请求
	@RequestMapping("deal_change")
	public ModelAndView deal_change(@RequestParam(value="information_id",required = true)int information_id,
			@RequestParam(value="user_id",required = true)int user_id) {
		
		ModelAndView modelAndView = new ModelAndView();
		Information information = informationService.selectById(information_id);
		if(information.getRelease_id()==user_id) {
			modelAndView.addObject("information", information);
			modelAndView.setViewName("common/change_infomation");
		}else {
			modelAndView.addObject("msg", "你没有权限修改该资讯");
			modelAndView.setViewName("common/error");
		}
		
		return modelAndView;
	}
	
	//真正修改资讯
	@RequestMapping("change")
	public @ResponseBody String change(@RequestParam(value="user_id",required = true)int user_id,
			Information information) {
		String msg = "fail";
		Information info_veryfiy = informationService.selectById(information.getId());
		if(info_veryfiy!=null) {
			//判断资讯所有者
			int release_id = information.getRelease_id();
			if(release_id == user_id && release_id == info_veryfiy.getRelease_id()) {
				int data = informationService.update(information);
				if(data >0) {
					msg="success";
				}
			}
		}
		
		return msg;
	}
	//发布资讯页面跳转
		@RequestMapping("deal_publishInfomation")
		public ModelAndView deal_publishInfomation(){
			ModelAndView modelAndView = new ModelAndView();
			List<Dictionary> infomation_type = dictionaryService.selectByType_code("ZD002");
			modelAndView.addObject("infomation_type", infomation_type);
			modelAndView.setViewName("common/publishInformation");
			return modelAndView;
		}
		//发布资讯
		@RequestMapping("publishInfomation")
		public ModelAndView publishInfomation(Information information){
			ModelAndView modelAndView = new ModelAndView();
			String msg="";
			information.setRelease_time(new Date());
			information.setModify_time(new Date());
			information.setStatus("2");
			information.setRead_count(0);
			int data = informationService.insert(information);
			if(data>0){
				msg="资讯发布成功";
			}else{
				msg="资讯发布失败";
			}
			modelAndView.addObject("msg", msg);
			modelAndView.setViewName("common/publishInformation");
			return modelAndView;
		}
		
		//我的资讯
		@RequestMapping("my_information")
		public ModelAndView my_information(int user_id,Args args,int pageindex, int pagesize){
			ModelAndView modelAndView = new ModelAndView();
			//分页开始
			PageHelper.startPage(pageindex, pagesize);
			List<Information> informations = informationService.selectByUser_id(user_id);
			PageInfo<Information> page = new PageInfo<Information>(informations);
			ListResponse<Information> json= FastResponse._().errCode(0).subCode(0).errMsg("获取购买需求成功")
					.args(args.getReqeustargs()).pageIndex(page.getPageNum())
					.pageSize(page.getSize()).total((int)page.getTotal()).pageCount(page.getPages())
					.hasNext(page.isHasNextPage()).list(informations);
			//分页结束
		
			modelAndView.addObject("json", json);
			modelAndView.setViewName("common/myInformation");
			return modelAndView;
		}
//		//资讯详情
//		@RequestMapping("information_detail")
//		public ModelAndView information_detail(int information_id){
//			ModelAndView modelAndView = new ModelAndView();
//			Information information = informationService.selectById(information_id);
//			String content = information.getContent();
//			//切割字符串以分页
//			String[] contents = content.split("_ueditor_page_break_tag_");
//			modelAndView.addObject("contents", contents);
//			
//			modelAndView.addObject("information", information);
//			modelAndView.setViewName("test/test");
//			return modelAndView;	
//		}
		//资讯浏览页面
		@RequestMapping("information")
		public ModelAndView information(Args args,int pageindex, int pagesize){

			ModelAndView modelAndView = new ModelAndView();
			
			//最新资讯
			List<Information> infoBytimeList=informationService.selectByTime();
			//潮流资讯--人气资讯
			List<Information> infoByCountList=informationService.selectByCount();
			
			//服装市场信息，处理拿到的资讯内容格式，分页
		PageHelper.startPage(pageindex, pagesize);
		List<Information> infoClothing=informationService.selectByType_code("ZD002002");
		//处理content中html标签问题
		for(Information info  : infoClothing){
			String content=info.getContent().replaceAll("</?[^>]+>", "").replaceAll("\\s*|\t|\r|\n", "");
			String c;
			if(content.length()<100){
				c=content;
			}else{
				c=content.substring(0,100);
			}
			info.setContent(c);	
			}
		PageInfo<Information> page = new PageInfo<Information>(infoClothing);
		 ListResponse<Information> json= FastResponse._().errCode(0).subCode(0).errMsg("分页成功")
				.args(args.getReqeustargs()).pageIndex(page.getPageNum())
				.pageSize(page.getSize()).total((int)page.getTotal()).pageCount(page.getPages())
				.hasNext(page.isHasNextPage()).list(infoClothing);
		 modelAndView.addObject("json", json);
		
			//流行趋势
			List<Information> infoNew=informationService.selectByType_code("ZD002001");

			modelAndView.addObject("infoBytimeList",infoBytimeList);
			modelAndView.addObject("infoByCountList",infoByCountList);
			modelAndView.addObject("infoClothing",infoClothing);
			modelAndView.addObject("infoNew",infoNew);
			modelAndView.setViewName("information/information");
			return modelAndView;	
		}
		
			//资讯浏览详细页面
				
				@RequestMapping("informationDetail")
				public ModelAndView informationDetail(int id,Args args,int pageindex, int pagesize){
					ModelAndView modelAndView = new ModelAndView();
					//获取资讯，并按分页符分页
					Information info=informationService.selectById(id);
					modelAndView.addObject("info", info);
					String content = info.getContent();
					//切割字符串以分页
						String[] contents = content.split("_ueditor_page_break_tag_");
						modelAndView.addObject("contents", contents);
					//获取评论，评论分页
						//分页开始
						PageHelper.startPage(pageindex, pagesize);
						List<Comment> comList=commentService.selectByObj_idAndTypeByTime(id, "4");
						PageInfo<Comment> page = new PageInfo<Comment>(comList);
						ListResponse<Comment> json= FastResponse._().errCode(0).subCode(0).errMsg("获取企业信息成功")
								.args(args.getReqeustargs()).pageIndex(page.getPageNum())
								.pageSize(page.getSize()).total((int)page.getTotal()).pageCount(page.getPages())
								.hasNext(page.isHasNextPage()).list(comList);
						//分页结束
						modelAndView.addObject("json", json);
					 //热点新闻
					 List<Information> hotInfoList=informationService.selectByCount();
					 modelAndView.addObject("hotInfoList", hotInfoList);
					//最新资讯
						List<Information> infoBytimeList=informationService.selectByTime();
						modelAndView.addObject("infoBytimeList", infoBytimeList);
						
					modelAndView.setViewName("information/informationDetail");
					return modelAndView;	
				}
				
				//资讯列表浏览服装市场页面
				@RequestMapping("informationFzsc")
				public ModelAndView informationFzsc(Args args,int pageindex, int pagesize){
					ModelAndView modelAndView = new ModelAndView();
					//服装市场信息,处理格式，分页
					PageHelper.startPage(pageindex, pagesize);
					List<Information> infoClothing=informationService.selectByType_code("ZD002002");
					for(Information info  : infoClothing){
						String content=info.getContent().replaceAll("</?[^>]+>", "").replaceAll("\\s*|\t|\r|\n", "");
						String c;
						if(content.length()<100){
							c=content;
						}else{
							c=content.substring(0,100);
						}
						info.setContent(c);	
						}
						PageInfo<Information> page = new PageInfo<Information>(infoClothing);
					 ListResponse<Information> json= FastResponse._().errCode(0).subCode(0).errMsg("分页成功")
							.args(args.getReqeustargs()).pageIndex(page.getPageNum())
							.pageSize(page.getSize()).total((int)page.getTotal()).pageCount(page.getPages())
							.hasNext(page.isHasNextPage()).list(infoClothing);
					 modelAndView.addObject("json", json);
					//潮流资讯--人气资讯
					List<Information> infoByCountList=informationService.selectByCount();
					//流行趋势
					List<Information> infoNew=informationService.selectByType_code("ZD002001");
			
					modelAndView.addObject("infoClothing",infoClothing);
					modelAndView.addObject("infoByCountList",infoByCountList);
					modelAndView.addObject("infoNew",infoNew);
					modelAndView.setViewName("information/informationfzsc");
					return modelAndView;	
				}
				
				//资讯列表浏览流行趋势页面
				@RequestMapping("informationLxqs")
				public ModelAndView informationLxqs(Args args,int pageindex, int pagesize){
					ModelAndView modelAndView = new ModelAndView();
					//流行趋势，处理格式，分页
					PageHelper.startPage(pageindex, pagesize);
					List<Information> infoNew=informationService.selectByType_code("ZD002001");
					for(Information info  : infoNew){
					//处理content中html标签问题
					String content=info.getContent().replaceAll("</?[^>]+>", "").replaceAll("\\s*|\t|\r|\n", "");
					String c;
					if(content.length()<100){
						c=content;
					}else{
						c=content.substring(0,100);
					}
					info.setContent(c);
					}
					PageInfo<Information> page = new PageInfo<Information>(infoNew);
					 ListResponse<Information> json= FastResponse._().errCode(0).subCode(0).errMsg("分页成功")
							.args(args.getReqeustargs()).pageIndex(page.getPageNum())
							.pageSize(page.getSize()).total((int)page.getTotal()).pageCount(page.getPages())
							.hasNext(page.isHasNextPage()).list(infoNew);
					 modelAndView.addObject("json", json);
					//潮流资讯--人气资讯
					List<Information> infoByCountList=informationService.selectByCount();
					//服装市场信息
					List<Information> infoClothing=informationService.selectByType_code("ZD002002");
					
					modelAndView.addObject("infoClothing",infoClothing);
					modelAndView.addObject("infoByCountList",infoByCountList);
					modelAndView.addObject("infoNew",infoNew);
					modelAndView.setViewName("information/informationlxqs");
					return modelAndView;	
				}
}
